package com.wlyuan.open.application.publisher;

import com.alibaba.fastjson.JSON;
import com.wlyuan.core.domain.event.DomainEvent;
import com.wlyuan.core.domain.event.DomainEventPublisher;
import com.wlyuan.core.domain.event.DomainEventTracer;
import com.wlyuan.rabbit.MessagePublisher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author yuanzheng
 */
@Service
@Slf4j
public class OpenPusherMessagePublisher implements DomainEventTracer {
    public final static String EXCHANGE_NAME_PUSHER = "exchange.topic.pusher";

    private final MessagePublisher messagePublisher;


    public OpenPusherMessagePublisher(MessagePublisher messagePublisher) {
        this.messagePublisher = messagePublisher;
        DomainEventPublisher.register(this);
    }

    @Override
    public void handle(DomainEvent event) {
        String routeKey = event.getClass().getSimpleName();
        logger.debug("开放平台发送消息队列，routeKey = {}, event = {}", routeKey, JSON.toJSON(event));
        messagePublisher.publish(event, EXCHANGE_NAME_PUSHER, routeKey);

    }
}
